<?php

namespace App\Http\Controllers\Api;

use App\Events\UserRegistered;
use App\Http\Requests\Api\LoginRequest;
use App\Http\Requests\Api\QuickLoginRequest;
use App\Http\Requests\Api\RegisterRequest;
use App\Models\Sms;
use App\Models\User;
use Carbon\Carbon;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Hash;

/**
 * @title 认证
 * @description 接口说明
 */
class AuthenticateController extends ApiController
{
    /**
     * @title 注册接口
     * @description 接口说明
     * @author wjx
     * @url /api/register
     * @method POST

     * @param name:phone type:string require:1 default: other: desc:手机号
     * @param name:password type:string require:1 default: other: desc:密码
     * @param name:security type:string require:1 default: other: desc:交易密码
     * @param name:sms_code type:string require:1 default: other: desc:短信验证码
     *
     */
    public function register(RegisterRequest $request, User $user)
    {
        $phone = $request->phone;
        $code = $request->sms_code;
        $check_sms = $this->checkSmsCode($phone, $code);
        if ($check_sms !== true) {
            return $this->failed($check_sms['message']);
        }
        $user->phone = $phone;
        $user->password = bcrypt($request->password);
        $user->security = bcrypt($request->security);
        $user->status = User::STATUS_ABLED;
        $user->code = $this->str_rand();
        $user->nickname = $this->get_name();
        $user->regType = 0;
        $user->hasPwd = 1;
        $user->hasPhone = 1;
        $user->hasPayPwd = 1;

        DB::beginTransaction();
        try {
            $user->save();
            event(new UserRegistered($user->id));
            DB::commit();
            return $this->message('注册成功');
        } catch (\Exception $exception) {
            DB::rollBack();
            return $this->failed('注册失败');
        }
    }

    /**
     * 生成邀请码
     *
     * @return void
     * @Description
     * @author wjx
     */
    public function str_rand()
    {
        $str = mt_rand(100000, 999999);
        $count = User::where('code', $str)->count();
        if ($count > 0) {
            return $this->str_rand();
        } else {
            return $str;
        }
    }
    //获取昵称
    public function get_name()
    {
        $nickname = '用户' . mt_rand(100000, 999999);
        $user = User::where('nickname', $nickname)->first();

        if ($user) {
            return $this->get_name();
        } else {
            return $nickname;
        }
    }

    /**
     * @title 登录接口
     * @description 接口说明
     * @author wjx
     * @url /api/login
     * @method POST
     *
     * @param name:phone type:string require:1 default: other: desc:手机号
     * @param name:password type:string require:1 default: other: desc:密码
     *
     * @return accessToken:accessToken
     * @return refreshToken:refreshToken
     * @return atExpiredTime:atExpiredTime
     * @return rtExpiredTime:rtExpiredTime
     * @return phone:phone
     * @return hasPwd:hasPwd
     * @return hasPhone:hasPhone
     * @return hasPayPwd:hasPayPwd
     *
     */
    public function login(LoginRequest $request)
    {
        $user = User::where('phone', $request->phone)
            ->orWhere('email', $request->email)
            ->first();

        if (!$user) {
            return $this->failed('用户不存在');
        }

        //被禁用做判断
        if ($user->status == User::STATUS_DISABLED) {
            return $this->failed('您的账号已被禁用！');
        }
        if (!Hash::check($request->password, $user->password)) {
            return $this->failed('密码错误');
        }

        $user->accessToken = strtolower(str_random(64));
        $user->refreshToken = strtolower(str_random(64));
        $user->atExpiredTime = Carbon::now()->addHours(2); //俩小时
        $user->rtExpiredTime = Carbon::now()->addDays(7); //七天
        $user->save();

        return $this->success([
            'accessToken' => $user->id . '.' . $user->accessToken,
            'refreshToken' => $user->id . '.' . $user->refreshToken,
            'atExpiredTime' => $user->atExpiredTime->toDateTimeString(),
            'rtExpiredTime' => $user->rtExpiredTime->toDateTimeString(),
            'user_id' => $user->id,
            'phone' => $user->phone,
            'hasPwd' => $user->hasPwd,
            'hasPhone' => $user->hasPhone,
            'hasPayPwd' => $user->hasPayPwd,
        ]);
    }
    /**
     * @title 刷新accessToken接口
     * @description 接口说明
     * @author wjx
     * @url /api/getRefreshToken
     * @method POST
     * @header name:refreshToken require:1 default: desc:refreshToken
     *
     */
    public function getRefreshToken(Request $request)
    {
        $refreshToken = $request->header('refreshToken');

        if (!($refreshToken)) {
            return response()->json([
                'code' => 1025,
                'message' => '请重新登录',
            ]);
            // return $this->failed('请传入refreshToken');
        }
        if (strpos($refreshToken, '.') === false) {
            return response()->json([
                'code' => 1025,
                'message' => '请重新登录',
            ]);
            // return $this->failed('refreshToken格式错误');
        }
        list($uid, $token) = explode('.', $refreshToken);
        $user = User::where('id', $uid)->where('refreshToken', $token)->first();
        if (!$user) {
            return response()->json([
                'code' => 1025,
                'message' => '请重新登录',
            ]);
        }

        $now = Carbon::now();
        $rtExpiredTime = $user->rtExpiredTime;
        if ($now->gt($rtExpiredTime)) {
            return response()->json([
                'code' => 1025,
                'message' => '请重新登录',
            ]);
        } else {
            $user->accessToken = strtolower(str_random(64));
            $user->refreshToken = strtolower(str_random(64));
            $user->atExpiredTime = Carbon::now()->addHours(2); //俩小时
            $user->rtExpiredTime = Carbon::now()->addDays(7); //七天
            $user->save();
            return $this->success([
                'accessToken' => $user->id . '.' . $user->accessToken,
                'refreshToken' => $user->id . '.' . $user->refreshToken,
                'atExpiredTime' => $user->atExpiredTime->toDateTimeString(),
                'rtExpiredTime' => $user->rtExpiredTime->toDateTimeString(),
                'user_id' => $user->id,
            ]);
        }
    }
    /**
     * @title 验证accessToken接口
     * @description 接口说明
     * @author wjx
     * @url /api/checkAccessToken
     * @method POST
     * @header name:accessToken require:1 default: desc:accessToken
     *
     */
    public function checkAccessToken(Request $request)
    {
        $accessToken = $request->header('accessToken');

        if (!$accessToken) {
            return $this->failed('请传入accessToken');
        }
        if (strpos($accessToken, '.') === false) {
            return $this->failed('accessToken格式错误');
        }
        list($uid, $accessToken) = explode('.', $accessToken);
        $user = User::where('id', $uid)->where('accessToken', $accessToken)->first();
        if (!$user) {
            return response()->json([
                'code' => 1025,
                'message' => 'accessToken作废',
            ]);
        }
        $now = Carbon::now();
        $atExpiredTime = $user->atExpiredTime;
        if ($now->gt($atExpiredTime)) {
            return response()->json([
                'code' => 1024,
                'message' => 'accessToken已过期，请重新登录',
            ]);
        }
        return $this->message('accessToken正常');
    }

    public function smsCode(Request $request)
    {
        $this->validate(
            request(),
            [
                'phone' => 'required|regex:/^1[34578][0-9]{9}$/',

                'captcha' => 'required|captcha_api:' . $request->key,
            ],
            [
                'phone.required' => '请输入手机号',
                'phone.regex' => '手机号格式不正确',

                'captcha.required' => '请输入验证码',
                'captcha.captcha_api' => '验证码错误',
            ]
        );
        $phone = $request->phone;

        $result = sendCodeAndSave($phone);

        if ($result['success']) {
            return $this->message($result['message']);
        } else {
            return $this->failed($result['message']);
        }
    }

    public function sms_for_quick(Request $request)
    {
        $this->validate(
            request(),
            [
                'captcha' => 'required|captcha_api:' . $request->key,
            ],
            [
                'captcha.required' => '请输入图片验证码',
                'captcha.captcha_api' => '验证码错误',
            ]
        );
        $phone = $request->phone;

        $result = sendCodeAndSave($phone);

        if ($result['success']) {
            return $this->message($result['message']);
        } else {
            return $this->failed($result['message']);
        }
    }

    /**
     * @title 退出登录
     * @description 接口说明
     * @author wjx
     * @url /api/logout
     * @method POST
     * @header name:accessToken require:1 default: desc:Token
     */

    public function logout(Request $request)
    {
        $user = $request->user;
        if (!$user) {
            return $this->failed('没有此用户');
        }
        $user->accessToken = strtolower(str_random(64));
        $user->refreshToken = strtolower(str_random(64));
        $user->atExpiredTime = Carbon::now()->addHours(2); //俩小时
        $user->rtExpiredTime = Carbon::now()->addDays(7); //七天
        $user->save();

        return $this->message('退出成功');
    }

    /**
     * @title 验证短信验证码
     * @description 接口说明
     * @author wjx
     * @url /api/verifySmsCode
     * @method POST

     * @param name:phone type:string require:1 default: other: desc:手机号
     *@param name:code type:string require:1 default: other: desc:验证码
     *
     */
    public function verifySmsCode(Request $request, Sms $sms)
    {
        $sms = Sms::where('phone',$request->phone)->orderBy('id','desc')->first();
        if ($request->code == $sms->code) {
            return $this->message('短信验证码正确');
        } else {
            return $this->failed('短信验证码错误');
        }
    }

    public function createCodeImage(\App\Handlers\GregwarHandler $Gregwar)
    {

        $res = $Gregwar->captcha();

        return $this->success($res);

    }

    public function verify_code_image(Request $request)
    {
        $this->validate(
            request(),
            [
                'captcha' => 'required|captcha_api:' . $request->key,
            ],
            [
                'captcha.required' => '请输入验证码',
                'captcha.captcha_api' => '验证码错误',
            ]
        );
        return true;
    }

    /**
     * @title 找回密码(短信)
     * @description 接口说明
     * @author cqd
     * @url /api/forgetPassword
     * @method POST
     * @param name:phone type:string require:1 default: other: desc:手机号
     * @param name:password type:string require:1 default: other: desc:密码
     */
    public function forgetPassword(Request $request, User $user)
    {
        $this->validate(
            request(),
            [
                'phone' => 'required|exists:users|regex:/^1[34578][0-9]{9}$/',
                'password' => 'required|min:6',
            ],
            [
                'phone.required' => '缺少手机号',
                'phone.exists' => '该账户不存在',
                'password.required' => '请输入登录密码',
                'password.min' => '密码最少6位',
            ]
        );

        $password = bcrypt($request->password);

        $update = User::where('phone', '=', $request->phone)->update(
            ['password' => $password]
        );

        if ($update) {
            return $this->message('更改成功');
        } else {
            return $this->failed('更改失败');
        }

    }
    /**
     * @title 找回支付密码(验证短信验证码)
     * @description 接口说明
     * @author wjx
     * @url /api/forgetSecurityCheckSms
     * @method POST
     * @header name:accessToken require:1 default: desc:Token
     * @param name:sms_code type:string require:1 default: other: desc:短信验证码
     */
    public function forgetSecurityCheckSms(Request $request, User $user)
    {
        $user = $request->user;
        $sms_code = $request->sms_code;
        if(!$sms_code){
            return $this->failed('缺少参数sms_code');
        }
        $sms = Sms::where('phone',$user->phone)->orderBy('id','desc')->first();
        if ($sms_code == $sms->code) {
            return $this->message('短信验证码正确');
        } else {
            return $this->failed('短信验证码错误');
        }
    }
    /**
     * @title 找回支付密码(短信)
     * @description 接口说明
     * @author wjx
     * @url /api/forgetSecurity
     * @method POST
     * @header name:accessToken require:1 default: desc:Token
     * @param name:sms_code type:string require:1 default: other: desc:短信验证码
     * @param name:security type:string require:1 default: other: desc:支付密码
     */
    public function forgetSecurity(Request $request, User $user)
    {
        $user = $request->user;
        $this->validate(
            request(),
            [
                'sms_code' => 'required',
                'security' => 'required|min:6',
            ],
            [
                'sms_code.required' => '缺少参数sms_code',
                'security.required' => '请输入支付密码',
                'security.min' => '支付密码至少6位',
            ]
        );
        $check_sms = $this->checkSmsCode($user->phone, $request->sms_code);
        if ($check_sms !== true) {
            return $this->failed($check_sms['message']);
        }
        $password = bcrypt($request->security);
        $update = User::where('phone', '=', $request->phone)->update(
            ['security' => $password]
        );
        if ($update) {
            return $this->message('更改成功');
        } else {
            return $this->failed('更改失败');
        }
    }
    /**
     * @title 验证支付密码
     * @description 接口说明
     * @author wjx
     * @url /api/checkSecurity
     * @method POST
     * @header name:accessToken require:1 default: desc:用户token
     * @param name:security type:string require:1 default: other: desc:支付密码
     */
    public function checkSecurity(Request $request)
    {
        $user = $request->user;
        $security = $request->security;
        if (!$security) {
            return $this->failed('请输入支付密码');
        }
        if (!Hash::check($request->security, $user->security)) {
            return $this->failed('原密码错误');
        } else {
            return $this->message('原密码正确');
        }
    }
    /**
     * @title 修改支付密码
     * @description 接口说明
     * @author cqd
     * @url /api/updateSecurity
     * @method POST
     * @header name:accessToken require:1 default: desc:用户token
     * @param name:security type:string require:1 default: other: desc:支付密码
     * @param name:new_security type:string require:1 default: other: desc:新的支付密码
     */
    public function updateSecurity(Request $request, User $user)
    {
        $this->validate(
            request(),
            [
                'security' => 'required',
                'new_security' => 'required|min:6',
            ],
            [
                'security.required' => '请输入支付密码',
                'new_security.required' => '请输入支新的支付密码',
                'new_security.min' => '支付密码至少6位',
            ]
        );

        $user_info = $request->user;

        if (!Hash::check($request->security, $user_info->security)) {
            return $this->failed('原密码错误');
        }

        $security = bcrypt($request->new_security);

        if ($user) {

            $update = User::where('phone', '=', $user_info->phone)->update(
                ['security' => $security, 'hasPayPwd' => 1]
            );
            if ($update) {
                return $this->message('更改成功');
            } else {
                return $this->failed('更改失败');
            }
        } else {
            return $this->failed('原密码错误');
        }

    }

    /**
     * @title 修改登录密码
     * @description 接口说明
     * @author wjx
     * @url /api/updatePassword
     * @method POST
     * @header name:accessToken require:1 default: desc:用户token
     * @param name:password type:string require:1 default: other: desc:登录密码
     * @param name:new_password type:string require:1 default: other: desc:新的登录密码
     */
    public function updatePassword(Request $request, User $user)
    {
        $this->validate(
            request(),
            [
                'password' => 'required',
                'new_password' => 'required|min:6',
            ],
            [
                'password.required' => '请输入登录密码',
                'new_password.required' => '请输入新的登录密码',
                'new_password.min' => '登录密码至少6位',
            ]
        );

        $user_info = $request->user;

        if (!Hash::check($request->password, $user_info->password)) {
            return $this->failed('原密码错误');
        }

        $password = bcrypt($request->new_password);

        
        $user_info->password = $password;
        $user_info->hasPwd = 1;

        if ($user_info->save()) {
            return $this->message('更改成功');
        } else {
            return $this->failed('更改失败');
        }

    }
    /**
     * @title 设置登录密码
     * @description 接口说明
     * @author wjx
     * @url /api/setPassword
     * @method POST
     * @header name:accessToken require:1 default: desc:用户token
     *
     * @param name:password type:string require:1 default: other: desc:登录密码
     */
    public function setPassword(Request $request, User $user)
    {
        $user = $request->user;
        $this->validate(
            request(),
            [
                'password' => 'required|min:6',
            ],
            [
                'password.required' => '请输入登录密码',
                'password.min' => '密码最少6位',
            ]
        );
        if($user->hasPwd && $user->password){
            return $this->failed('登录密码已设置');
        }
        $password = bcrypt($request->password);
        $user->password = $password;
        $user->hasPwd = 1;

        if ($user->save()) {
            return $this->message('设置密码成功');
        } else {
            return $this->failed('设置密码失败');
        }
    }
    /**
     * @title 设置支付密码
     * @description 接口说明
     * @author wjx
     * @url /api/setPayPassword
     * @method POST
     * @header name:accessToken require:1 default: desc:用户token
     *
     * @param name:security type:string require:1 default: other: desc:支付密码
     */
    public function setPayPassword(Request $request, User $user)
    {
        $user = $request->user;
        $this->validate(
            request(),
            [
                'security' => 'required|min:6',
            ],
            [
                'security.required' => '请输入支付密码',
                'security.min' => '密码最少6位',
            ]
        );
        if($user->hasPayPwd && $user->security){
            return $this->failed('支付密码已设置');
        }
        $security = bcrypt($request->security);
        $user->security = $security;
        $user->hasPayPwd = 1;

        if ($user->save()) {
            return $this->message('设置支付密码成功');
        } else {
            return $this->failed('设置支付密码失败');
        }
    }
    /**
     * @title 重置登录密码
     * @description 接口说明
     * @author wjx
     * @url /api/resetPassword
     * @method POST
     * @header name:accessToken require:1 default: desc:用户token
     *
     *
     * @param name:phone type:string require:1 default: other: desc:手机号
     * @param name:sms_code type:string require:1 default: other: desc:短信验证码
     * @param name:password type:string require:1 default: other: desc:新登录密码
     */
    public function resetPassword(Request $request, User $user)
    {
        $this->validate(
            request(),
            [
                'phone' => 'required|exists:users|regex:/^1[34578][0-9]{9}$/',
                'sms_code' => 'required',
                'password' => 'required|min:6',
            ],
            [
                'phone.required' => '请输入手机号',
                'phone.exists' => '该账户不存在',
                'sms_code.required' => '请输入短信验证码',
                'password.required' => '请输入新登录密码',
                'password.min' => '密码最少6位',
            ]
        );
        $check_sms = $this->checkSmsCode($request->phone, $request->sms_code);
        if ($check_sms !== true) {
            return $this->failed($check_sms['message']);
        }

        $password = bcrypt($request->password);
        $user = User::where('phone', '=', $request->phone)->first();
        $user->password = $password;
        $user->hasPwd = 1;

        if ($user->save()) {
            return $this->message('重置密码成功');
        } else {
            return $this->failed('重置失败');
        }

    }

    public function resetPayPassword(Request $request, User $user)
    {
        $this->validate(
            request(),
            [
                'phone' => 'required|exists:users|regex:/^1[34578][0-9]{9}$/',
                'sms_code' => 'required',
                'security' => 'required|min:6',
            ],
            [
                'phone.required' => '请输入手机号',
                'phone.exists' => '该账户不存在',
                'sms_code.required' => '请输入短信验证码',
                'security.required' => '请输入支付密码',
                'security.min' => '密码最少6位',
            ]
        );
        $check_sms = $this->checkSmsCode($request->phone, $request->sms_code);
        if ($check_sms !== true) {
            return $this->failed($check_sms['message']);
        }

        $security = bcrypt($request->security);
        $user = User::where('phone', '=', $request->phone)->first();
        $user->security = $security;
        $user->hasPayPwd = 1;

        if ($user->save()) {
            return $this->message('设置支付密码成功');
        } else {
            return $this->failed('设置支付密码失败');
        }

    }
    /**
     * @title 验证旧支付密码
     * @description 接口说明
     * @author wjx
     * @url /api/checkPayPassword
     * @method POST
     * @header name:accessToken require:1 default: desc:用户token
     *
     *
     * @param name:phone type:string require:1 default: other: desc:手机号
     * @param name:sms_code type:string require:1 default: other: desc:短信验证码
     * @param name:security type:string require:1 default: other: desc:支付密码
     */
    public function checkPayPassword(Request $request, User $user)
    {
        $this->validate(
            request(),
            [
                'phone' => 'required|exists:users|regex:/^1[34578][0-9]{9}$/',
                'sms_code' => 'required',
                'security' => 'required|min:6',
            ],
            [
                'phone.required' => '请输入手机号',
                'phone.exists' => '该账户不存在',
                'sms_code.required' => '请输入短信验证码',
                'security.required' => '请输入支付密码',
                'security.min' => '密码最少6位',
            ]
        );
        $check_sms = $this->checkSmsCode($request->phone, $request->sms_code);
        if ($check_sms !== true) {
            return $this->failed($check_sms['message']);
        }
        $user = User::where('phone', $request->phone)->first();
        if (!Hash::check($request->security, $user->security)) {
            return $this->failed('原支付密码错误');
        } else {
            return $this->message('原支付密码正确');
        }
    }

    /**
     * @title 快速登录
     * @description 接口说明
     * @author wjx
     * @url /api/quickLogin
     * @method POST

     * @param name:phone type:string require:1 default: other: desc:手机号
     * @param name:code type:string require:1 default: other: desc:验证码
     *
     */
    public function quickLogin(Request $request, User $user, Sms $sms)
    {
        $user = User::where('phone', $request->phone)->first();
        $sms = Sms::where('phone', $request->phone)->orderBy('id', 'desc')->first();
        if ($sms) {
            if ($request->code != $sms->code) {
                return $this->failed('短信验证失败');
            }
        } else {
            return $this->failed('短信验证失败');
        }
        $user->accessToken = strtolower(str_random(64));
        $user->refreshToken = strtolower(str_random(64));
        $user->atExpiredTime = Carbon::now()->addHours(2); //俩小时
        $user->rtExpiredTime = Carbon::now()->addDays(7); //七天
        $user->save();
        return $this->success([
            'accessToken' => $user->id . '.' . $user->accessToken,
            'refreshToken' => $user->id . '.' . $user->refreshToken,
            'atExpiredTime' => $user->atExpiredTime->toDateTimeString(),
            'rtExpiredTime' => $user->rtExpiredTime->toDateTimeString(),
            'user_id' => $user->id,
            'phone' => $user->phone,
        ]);

    }
    /**
     * @title 快速登录(新)
     * @description 接口说明
     * @author wjx
     * @url /api/quickLoginNew
     * @method POST

     * @param name:phone type:string require:1 default: other: desc:手机号
     * @param name:key type:string require:1 default: other: desc:验证key
     * @param name:captcha type:string require:1 default: other: desc:图片验证码
     * @param name:sms_code type:string require:1 default: other: desc:短信验证码
     *
     */
    public function quickLoginNew(Request $request, QuickLoginRequest $quickrequest)
    {
        $phone = $quickrequest->phone;
        $code = $quickrequest->sms_code;
        $check_sms = $this->checkSmsCode($phone, $code);
        if ($check_sms !== true) {
            return $this->failed($check_sms['message']);
        }
        $user = User::where('phone', $phone)->first();
        if ($user) {
            $user->accessToken = strtolower(str_random(64));
            $user->refreshToken = strtolower(str_random(64));
            $user->atExpiredTime = Carbon::now()->addHours(2); //俩小时
            $user->rtExpiredTime = Carbon::now()->addDays(7); //七天
            $user->save();
            return $this->success([
                'accessToken' => $user->id . '.' . $user->accessToken,
                'refreshToken' => $user->id . '.' . $user->refreshToken,
                'atExpiredTime' => $user->atExpiredTime->toDateTimeString(),
                'rtExpiredTime' => $user->rtExpiredTime->toDateTimeString(),
                'user_id' => $user->id,
                'hasPwd' => $user->hasPwd,
                'hasPhone' => $user->hasPhone,
                'hasPayPwd' => $user->hasPayPwd,
            ]);
        } else {
            $user = new User();
            $user->phone = $phone;
            $user->status = User::STATUS_ABLED;
            $user->code = $this->str_rand();
            $user->nickname = $this->get_name();
            $user->regType = 1;
            $user->hasPwd = 0;
            $user->hasPhone = 1;
            $user->hasPayPwd = 0;
            $user->accessToken = strtolower(str_random(64));
            $user->refreshToken = strtolower(str_random(64));
            $user->atExpiredTime = Carbon::now()->addHours(2); //俩小时
            $user->rtExpiredTime = Carbon::now()->addDays(7); //七天
            $user->save();
            event(new UserRegistered($user->id));
            return $this->success([
                'accessToken' => $user->id . '.' . $user->accessToken,
                'refreshToken' => $user->id . '.' . $user->refreshToken,
                'atExpiredTime' => $user->atExpiredTime->toDateTimeString(),
                'rtExpiredTime' => $user->rtExpiredTime->toDateTimeString(),
                'user_id' => $user->id,
                'hasPwd' => $user->hasPwd,
                'hasPhone' => $user->hasPhone,
                'hasPayPwd' => $user->hasPayPwd,
            ]);
        }
    }

    /**
     * @title 验证旧手机号
     * @description 接口说明
     * @author wjx
     * @url /api/checkPhone
     * @method POST
     * @header name:accessToken require:1 default: desc:accessToken
     *
     * @module 修改绑定手机号
     * @param name:sms_code type:string require:1 default: other: desc:短信验证码
     *
     */
    public function checkPhone(Request $request)
    {
        $user = $request->user;
        $sms_code = $request->sms_code;
        $user_phone = $user->phone;
        if (!$sms_code) {
            return $this->failed('缺少参数');
        }
        if (!$user_phone) {
            return $this->failed('请先绑定手机号');
        }
        $res = Sms::where('phone', $user_phone)->orderBy('id', 'desc')->first();
        if ($sms_code !== $res->code) {
            return $this->failed('短信验证码错误');
        }

        return $this->message('验证通过');
    }
    /**
     * @title 绑定新手机号
     * @description 接口说明
     * @author wjx
     * @url /api/bindNewPhone
     * @method POST
     * @header name:accessToken require:1 default: desc:accessToken
     *
     * @module 修改绑定手机号
     *
     * @param name:new_phone type:string require:1 default: other: desc:新手机号
     * @param name:old_sms_code type:string require:1 default: other: desc:原手机号短信验证码
     * @param name:new_sms_code type:string require:1 default: other: desc:新手机号短信验证码
     *
     */
    public function bindNewPhone(Request $request)
    {
        $user = $request->user;
        $old_phone = $user->phone;
        $new_phone = $request->new_phone;
        $old_sms_code = $request->old_sms_code;
        $new_sms_code = $request->new_sms_code;
        if (!$new_phone || !$old_sms_code || !$new_sms_code) {
            return $this->failed('缺少参数');
        }

        $check_sms_new = $this->checkSmsCode($new_phone, $new_sms_code);
        if ($check_sms_new !== true) {
            return $this->failed($check_sms_new['message']);
        }
        $check_sms_old = $this->checkSmsCode($old_phone, $old_sms_code);
        if ($check_sms_old !== true) {
            return $this->failed($check_sms_old['message']);
        }
        $has_user = User::where('phone', $new_phone)->first();
        if (!$has_user) {
            return $this->failed('该手机号已经注册');
        }
        $user->phone = $new_phone;
        $user->hasPhone = 1;
        $user->save();

        return $this->message('修改手机号成功');

    }

}
